package com.harmontronics.hanma.paas.paas.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.annotation.TokenValidSkip;
import com.harmontronics.hanma.paas.paas.domain.job.entity.JobListPage;
import com.harmontronics.hanma.paas.paas.domain.job.service.helper.CronUtil;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.HmUser;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.UserListPage;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import cn.hutool.core.convert.Convert;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmJobAppService;
import com.harmontronics.hanma.paas.paas.dto.job.output.HmJobResult;
import com.harmontronics.hanma.paas.paas.dto.job.input.HmJobCreateInput;
import com.harmontronics.hanma.paas.paas.dto.job.input.HmJobUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.job.entity.HmJob;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * ${comments}
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-21 14:42:39
 */
@RestController
@RequestMapping("/api/hmjob")
@Api(tags = {"定时任务"})
public class HmJobController {
    @Autowired
    private HmJobAppService hmJobappService;


    /**
     * 分页列表
     *
     *
     */
    @GetMapping(value = "/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmJob>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize,
            @RequestParam(required = false) String jobName,
            @RequestParam(required = false) String jogGroup,
            @RequestParam(required = false) String misfirePolicy,
            @RequestParam(required = false) String concurrent,
            @RequestParam(required = false) String status) {
        JobListPage<HmJob> pageable = new JobListPage(pageNo, pageSize);
        pageable.setJobName(jobName).setJobGroup(jogGroup).setMisfirePolicy(misfirePolicy).setConcurrent(concurrent).setStatus(status);
        PageBase<HmJob> hmJobPageBase = hmJobappService.getPageUserWithParams(pageable);

        return ResponseEntity.ok(hmJobPageBase);
    }



    @GetMapping(value = "/info")
    @ApiOperation("任务信息")
    public ResponseEntity<HmJobResult> queryById(@RequestParam String id) {
        HmJob hmJob =hmJobappService.findById(id);
        HmJobResult result = BeanUtil.toBean(hmJob,HmJobResult.class);
        return ResponseEntity.ok(result);
    }


    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    @TokenValidSkip
    public ResponseEntity<String> save(@Valid @RequestBody HmJobCreateInput hmJobInput) throws SchedulerException {
        HmJob hmJob =BeanUtil.toBean(hmJobInput,HmJob. class);
        hmJobappService.saveJob(hmJob);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmJobUpdateInput hmJobInput)  throws SchedulerException{
        HmJob hmJob =BeanUtil.toBean(hmJobInput,HmJob. class);
        hmJobappService.updateJob(hmJob);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody String[] jobIds) throws SchedulerException{
        hmJobappService.delete(Arrays.asList(jobIds));
        return ResponseEntity.ok();
    }

    @PutMapping("/changeStatus")
    @ApiOperation("更新状态")
    public ResponseEntity changeStatus(@RequestParam String jobId) throws SchedulerException {
        hmJobappService.changeStatus(jobId);
        return ResponseEntity.ok();
    }


    @PutMapping("/run")
    @ApiOperation("运行任务")
    public ResponseEntity run(@RequestParam String jobId) throws SchedulerException {
        hmJobappService.run(jobId);
        return ResponseEntity.ok();
    }

    @GetMapping("/validate-cron")
    @ApiOperation("验证cron表达式")
    @TokenValidSkip
    public ResponseEntity<Boolean> validateCron(@RequestParam String cron){
        return ResponseEntity.ok(CronUtil.isValid(cron));
    }


}
